"""
处理功率相关数据
"""
import re

from excutor.excutor import Excutor
from dealNums import get_numbers

class Power(Excutor):
    def __init__(self, data):
        super().__init__(data)
        self.power = '功率'
    
    @staticmethod
    def Existence(data,power = '功率'):
        return data[data['new_key'].str.contains(power)]['new_key'].count()
    
    #修改数据表
    def processingData(self):
        indexSize = self.data[self.data['new_key'].str.contains(self.power)].index
        for index in indexSize:
            newValueRef = self._get_new_value(index)
            self.data.loc[index,'new_value_ref'] = newValueRef
        return self.data
    
    #分析并处理old_value,返回new_value_ref
    def _get_new_value(self,index):
        oneRowData = self.data.loc[index]
        old_value = oneRowData["old_value"]
        units = re.findall(r'[µmkMK]?[Ww]|dBm|[µm]|VA|J',old_value)
        numbers = get_numbers(old_value)
        if not units:
            units = re.findall(r'[µmkMK]?[Ww]|dBm|[µm]|VA|J',oneRowData["new_key"])
        if not numbers:
            return old_value
        elif len(numbers) == 1:
            if units:
                return '{!s}{!s}'.format(numbers[0],units[0])
            else:
                return '{!s}W'.format(numbers[0])
        else :
            if units:
                return '{!s} to {!s}{!s}'.format(numbers[0],numbers[-1],units[0])
            else:
                return '{!s} to {!s}W'.format(numbers[0],numbers[-1])
        